﻿using System;
using System.Collections.Generic;
using System.Text;
using ARCam3D.Tuple;

namespace ARCam3D.Transform
{
    public class MatrixTransform3D
    {
        private const int matrixColumnsCount = 4, matrixRowsCount = 3;

        private double[,] matrix;

        public MatrixTransform3D()
        {
            this.matrix = new double[matrixRowsCount, matrixColumnsCount];
        }

        public MatrixTransform3D(double[,] matrix)
        {
            this.matrix = new double[matrixRowsCount, matrixColumnsCount];
            for (int i = 0; i < matrixRowsCount; ++i)
            {
                for (int j = 0; j < matrixColumnsCount; ++j)
                {
                    this.matrix[i, j] = matrix[i, j];
                }
            }
        }

        public double this[int i, int j]
        {
            get { return matrix[i, j]; }
            set { matrix[i, j] = value; }
        }

        public void Transform(Tuple3D tuple)
        {
            double x = tuple.x;
            double y = tuple.y;
            double z = tuple.z;

            tuple.x = matrix[0, 0] * x + matrix[0, 1] * y + matrix[0, 2] * z + matrix[0, 3];
            tuple.y = matrix[1, 0] * x + matrix[1, 1] * y + matrix[1, 2] * z + matrix[1, 3];
            tuple.z = matrix[2, 0] * x + matrix[2, 1] * y + matrix[2, 2] * z + matrix[2, 3];
        }
    }
}
